xquery version "1.0-ml";

import module namespace ox="http://localhost/ns/ox" at "/porvino/lib/ox/ox.xqy";
declare namespace pv="http://localhost/porvino";

(: 
inserts a record for a new entry
then returns the edit page for that entry
:)

declare function pv:addWine($id) {
	let $wnname := xdmp:get-request-field("name")
	let $wnvintage := xdmp:get-request-field("vintage")
	let $winery := xdmp:get-request-field("winery")
	let $appellation := xdmp:get-request-field("appellation")
	let $vineyard := xdmp:get-request-field("vineyard")
	let $varietal := xdmp:get-request-field("varietal")
	
	let $bv1 := xdmp:get-request-field("blendVarietal1")
	let $bp1 := xdmp:get-request-field("blendPercentage1")
	
	let $onhand := xdmp:get-request-field("onhand")
	let $qty := xdmp:get-request-field("quantity")
	let $loc := xdmp:get-request-field("location")
	let $bottleSize := xdmp:get-request-field("bottleSize")
	let $cost := xdmp:get-request-field("cost")
	let $purchaseDate := xdmp:get-request-field("date")
	let $future := if (xdmp:get-request-field("future") = "future") then "true" else "false"
	let $wish := if (xdmp:get-request-field("wish") = "wish") then "true" else "false"
	let $matstart := xdmp:get-request-field("start")
	let $matend := xdmp:get-request-field("end")
	let $alc := xdmp:get-request-field("alcohol")
	let $sug := xdmp:get-request-field("sugar")
	let $cases := xdmp:get-request-field("cases")
	let $gennotes := xdmp:get-request-field("generalNotes")
	let $currentdatetime := fn:current-dateTime() 

	return
	xdmp:document-insert(xs:string($id),
	<wine id="{$id}" dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}" dateViewed="{$currentdatetime}">{"
		"}
		<name dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$wnname}</name>{"
		"}
		<vintage dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$wnvintage}</vintage>{"
		"}
		<winery dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$winery}</winery>{"
		"}
		<appellation dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$appellation}</appellation>{"
		"}
		<vineyard dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$vineyard}</vineyard>{"
		"}
		<varietal dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$varietal}</varietal>{"
		"}
		<blend dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{"
		"}
			<blendPart dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{"
		"}
				<blendVarietal dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$bv1}</blendVarietal>{"
		"}
				<blendPercentage dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$bp1}</blendPercentage>{"
		"}
			</blendPart>{"
		"}
		</blend>{"
		"}
		<alcohol dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$alc}</alcohol>{"
		"}
		<sugar dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$sug}</sugar>{"
		"}
		<inventory dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{"
        "}
            <inventoryEntry dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{"
            "}
        		<cases dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$cases}</cases>{"
        		"}
        		<onhand dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$onhand}</onhand>{"
                "}
        		<quantity dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$qty}</quantity>{"
        		"}
        		<location dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$loc}</location>{"
        		"}
        		<size dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$bottleSize}</size>{"
        		"}
        		<cost dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$cost}</cost>{"
        		"}
        		<date dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$purchaseDate}</date>{"
        		"}
        		<future dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$future}</future>{"
        		"}
        		<wish dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$wish}</wish>{"
                "}
            </inventoryEntry>{"
            "}
        </inventory>{"
        "}
		<start dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$matstart}</start>{"
		"}
		<end dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$matend}</end>{"
	    "}
		<generalNotes dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{$gennotes}</generalNotes>{"
		"}
		<notes dateCreated="{$currentdatetime}" dateModified="{$currentdatetime}">{"

		"}</notes>{"
	"}
	</wine>
	)
	,
	$id
};


xdmp:redirect-response( concat("/porvino/edit/editWine.xqy?id=", pv:addWine(ox:getNewId()) ) )


